//
//  ViewController.m
//  SMSTest
//
//  Created by mac on 16/1/20.
//  Copyright © 2016年 mac. All rights reserved.
//


/**
 *  定时器
 *  button
 *  可能存在的问题，
 *  在页面切换的时候，定时器是否能行
 *  
 */

#import "ViewController.h"
#import <SMS_SDK/SMSSDK.h>
@interface ViewController ()

{
    NSTimer *_tiemr;
    UIButton *_button_getyzm;
    int _num;
}
@property (weak, nonatomic) IBOutlet UITextField *text;
@property (weak, nonatomic) IBOutlet UITextField *code;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    _button_getyzm = [UIButton buttonWithType:UIButtonTypeCustom];
    _button_getyzm.frame= CGRectMake(20+40, 64+20+50+15+45+40+200, 100, 30);
    _button_getyzm.tag=222;
    [_button_getyzm setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    _button_getyzm.titleLabel.font = [UIFont systemFontOfSize:12];
    _button_getyzm.backgroundColor= [UIColor greenColor];
    [_button_getyzm addTarget:self action:@selector(getyzm) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button_getyzm];

    
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)wad{
    [_tiemr invalidate];
    _tiemr= [NSTimer  scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeButtonName) userInfo:nil repeats:YES];
    _button_getyzm.enabled = NO;
    [_button_getyzm setTitle:@"10秒后重新获取" forState:UIControlStateNormal];
    [_button_getyzm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _num = 10;
}

- (void)changeButtonName{
    if (_num >1) {
        _button_getyzm.enabled = NO;
        _num--;
        NSString * string = [NSString stringWithFormat:@"%d秒后重新获取",_num];
        [_button_getyzm setTitle:string forState:UIControlStateNormal ] ;
    }
    else
    {
        [_tiemr invalidate];
        [_button_getyzm setTitle:@"重新获取密码" forState:UIControlStateNormal];
        _button_getyzm.enabled = YES;
    }
}

-(void)getyzm
{
    
    [_tiemr invalidate];
    [self wad];
}


- (void)Sms
{
    
    /**
     *  @from                    v1.1.1
     *  @brief                   获取验证码(Get verification code)
     *
     *  @param method            获取验证码的方法(The method of getting verificationCode)
     *  @param phoneNumber       电话号码(The phone number)
     *  @param zone              区域号，不要加"+"号(Area code)
     *  @param customIdentifier  自定义短信模板标识 该标识需从官网http://www.mob.com上申请，审核通过后获得。(Custom model of SMS.  The identifier can get it  from http://www.mob.com  when the application had approved)
     *  @param result            请求结果回调(Results of the request)
     */

    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_text.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
                     NSLog(@"获取验证码成功");
                 } else {
                     NSLog(@"错误信息：%@",error);
            
        }
    }];
}

- (void)smstext
{
    /**
     * @from               v1.1.1
     * @brief              提交验证码(Commit the verification code)
     *
     * @param code         验证码(Verification code)
     * @param phoneNumber  电话号码(The phone number)
     * @param zone         区域号，不要加"+"号(Area code)
     * @param result       请求结果回调(Results of the request)
     */
    [SMSSDK commitVerificationCode:_code.text phoneNumber:_text.text zone:@"86" result:^(NSError *error) {
        if (!error) {
            NSLog(@"验证成功");
        }
        else
        {
            NSLog(@"错误信息：%@",error);
        }
    }];
}
- (IBAction)send:(id)sender {
    [self Sms];
}
- (IBAction)yesok:(id)sender {
    [self smstext];
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
